package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CategoryMenuMapper;
import com.itheima.pojo.CategoryMenu;
import com.itheima.pojo.TCategoryMenu;
import com.itheima.service.CategoryMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class CategoryMenuServiceImpl implements CategoryMenuService {

    @Autowired
    private CategoryMenuMapper categoryMenuMapper;

    @Override
    public void add(TCategoryMenu tCategoryMenu) {
        categoryMenuMapper.insert(tCategoryMenu);
    }

    @Override
    public void delete(TCategoryMenu tCategoryMenu) {
        categoryMenuMapper.deleteByPrimaryKey(tCategoryMenu.getUid());
    }

    @Override
    public void edit(TCategoryMenu tCategoryMenu) {
        categoryMenuMapper.updateByPrimaryKeySelective(tCategoryMenu);
    }

    @Override
    public List<TCategoryMenu> getAll(String keyword) {
        return categoryMenuMapper.selectAll();
    }

    @Override
    public List<TCategoryMenu> getMenulevel(Integer key) {
        Example example = new Example(TCategoryMenu.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("menuLevel",key);
        return categoryMenuMapper.selectByExample(example);
    }

    @Override
    public List<TCategoryMenu> getbutten() {
        Example example = new Example(TCategoryMenu.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("menuType",1);
        return categoryMenuMapper.selectByExample(example);
    }


    @Override
    public List<TCategoryMenu> getButtonAll(Integer keyword) {
        return categoryMenuMapper.selectByMenuLevel(keyword);
    }


    @Override
    public void stick(TCategoryMenu tCategoryMenu) {
        Integer maxSort = categoryMenuMapper.getMaxSort();
        tCategoryMenu.setSort(maxSort + 1);
        categoryMenuMapper.updateByPrimaryKeySelective(tCategoryMenu);
    }

    @Override
    public Page<TCategoryMenu> getList(Map<String, Object> searchMap) {
        Integer currentPage = (Integer) searchMap.get("currentPage");
        if (currentPage == null) {
            currentPage = 1;
        }
        Integer pageSize = (Integer) searchMap.get("pageSize");
        if (pageSize == null) {
            pageSize = 4;
        }
        PageHelper.startPage(currentPage, pageSize);
        Example example = createExample(searchMap);
        return (Page<TCategoryMenu>) categoryMenuMapper.selectByExample(example);
    }


    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(TCategoryMenu.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            if (searchMap.get("keyword") != null && !"".equals(searchMap.get("keyword"))) {
                criteria.andLike("name", "%" + searchMap.get("keyword") + "%");
            }
            if (searchMap.get("icon") != null && !"".equals(searchMap.get("icon"))) {
                criteria.andEqualTo("icon", searchMap.get("icon"));
            }
            if (searchMap.get("isShow") != null && !"".equals(searchMap.get("isShow"))) {
                criteria.andEqualTo("isShow", searchMap.get("isShow"));
            }
            if (searchMap.get("menuLevel") != null && !"".equals(searchMap.get("menuLevel"))) {
                criteria.andEqualTo("menuLevel", searchMap.get("menuLevel"));
            }
            if (searchMap.get("menuType") != null && !"".equals(searchMap.get("menuType"))) {
                criteria.andEqualTo("menuType", searchMap.get("menuType"));
            }
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andEqualTo("name", searchMap.get("name"));
            }
            if (searchMap.get("parentUid") != null && !"".equals(searchMap.get("parentUid"))) {
                criteria.andEqualTo("parentUid", searchMap.get("parentUid"));
            }
            if (searchMap.get("url") != null && !"".equals(searchMap.get("url"))) {
                criteria.andEqualTo("url", searchMap.get("url"));
            }
            if (searchMap.get("summary") != null && !"".equals(searchMap.get("summary"))) {
                criteria.andEqualTo("summary", searchMap.get("summary"));
            }
            if (searchMap.get("uid") != null && !"".equals(searchMap.get("uid"))) {
                criteria.andEqualTo("uid", searchMap.get("uid"));
            }
            if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }
            if (searchMap.get("sort") != null && !"".equals(searchMap.get("sort"))) {
                criteria.andEqualTo("sort", searchMap.get("sort"));
            }
        }
        return example;
    }


}
